< previous page page_53 next page >

Page 53
Puzzle 12
Registry Games, Part 2
Now that you know how to open a registry key, the next step is to enumerate its subkeys and their values. The main functions for enumerating key information are the RegEnumKey and RegEnumValue functions. Win32 also supports a function called RegEnumKeyEx, which allows you to retrieve additional information about keys, such as the last modification time for the subkey, but for our purposes RegEnumKey is fine. Let's take a quick look at the C declaration for the RegEnumKey function:
LONG RegEnumKey(
    HKEY hKey,                  // handle of key to query
    DWORD dwIndex,              // index of subkey to query
    LPTSTR lpName,              // address of buffer for subkey name
    DWORD cbName                // size of subkey buffer
   );
We'll look at the VB declaration for this function in Puzzle 13. For now, consider how this function works. The hKey parameter is obviousit's a handle to the key whose subkeys you wish to enumerate. The dwIndex parameter is the index of the subkey whose name you want to retrieve. The lpName parameter is a buffer to load with the subkey name. It will clearly be a string parameter, and since the API function loads the string with data, the string variable must be initialized to a length that is long enough to hold the key name.
Clearly, one way to use this function would be to call it in a loop, incrementing the dwIndex parameter each time, until an error occurs. The lpName parameter could be initialized to some very large value (the Win32 API documentation says that this parameter need never be larger than MAX_PATH + 1 characters or 261 bytes).
Another approach to this function would be to determine ahead of time the maximum key name length and how many subkeys are present. This can be accomplished using the RegQueryInfoKey function. This function is declared as follows:
LONG RegQueryInfoKey (
    HKEY hKey,                  // handle of key to query
    LPTSTR lpClass,             // address of buffer for class string
    LPDWORD lpcbClass,          // address of size of class string buffer

 
< previous page page_53 next page >